package lib.reflection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import lib.Field;

public class Reflector {
	
	public static Object setReflectedMethods(List<Field> fields, Object obj, Class cls) throws NoSuchMethodException, IllegalAccessException,
			InvocationTargetException {
		Class argType[] = new Class[1];
		argType[0] = String.class;
		
		Method[] methList = cls.getDeclaredMethods();
		ArrayList<String> methodNames = new ArrayList<String>();
		for (Method method : methList) {
			methodNames.add(method.getName());
		}
		for (Field field : fields) {			
			String methodName = "set"+field.getName();
			if (!methodNames.contains(methodName))
				continue;
		//	System.out.println(String.format("setting... '%s' with value '%s'",methodName,field.value));
			Method method = cls.getMethod(methodName, argType);
			method.invoke(obj,field.getValue());
		}
		return obj;
	}
}
